<template>
  <el-dialog width="600px" :visible.sync="isshow" :show-close="false" class="register">
    <div class="title" slot="title">用户注册</div>
    <el-form :model="form" label-width="80px" ref="form" :rules="rules">
      <el-form-item prop="avatar" label="头像" class="lable-img">
        <el-upload
          class="avatar-uploader"
          :action="baseurl+'/uploads'"
          :show-file-list="false"
          name="image"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="baseurl+'/'+imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="username" label="昵称">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email"></el-input> 
      </el-form-item>
      <el-form-item prop="phone" label="手机">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item prop="code" label="图形码">
        <el-row>
          <el-col :span="16">
            <el-input v-model="form.code"></el-input>
          </el-col>
          <el-col :span="7" :offset="1">
            <img class="img-code" @click="codeimgclick" :src="baseurl+codeurl" alt />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item prop="rcode" label="验证码">
        <el-row>
          <el-col :span="16">
            <el-input v-model="form.rcode"></el-input>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-button :disabled="totalTime != 5" @click="getcode">获取用户验证码<span v-if="totalTime!=5">({{totalTime}})</span></el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer">
      <el-button type="primary" @click="sblink">确定</el-button>
      <el-button @click="isshow=false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getphonecode,toregister} from '@/api/register.js'
export default {
  watch:{
    isshow(newval){
      if(newval==false){
        this.$refs.form.resetFields()
        this.imageUrl=''
      }
    }
  },
  data() {
    return {
      totalTime:5,
      form: {
        avatar: "", //	是	string	头像地址
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        password: "", //	是	string	密码
        rcode: "", //	是	string	验证码
        code: "",
      },
      rules: {
        avatar: [{ required: true, message: "请上传头像", trigger: "change" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "change" },
        {validator:(rule,value,callback)=>{
          let reg=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
          if(reg.test(value)){
            callback()
          }else{
            callback(new Error("请输入正确的邮箱"))
          }
        },
        trigger:"change"
        }
        ],
        phone: [{ required: true, message: "请输入手机号", trigger: "change" },
          {validator:(rule,value,callback)=>{
          let reg=/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
          if(reg.test(value)){
            callback()
          }else{
            callback(new Error("请输入正确的手机号"))
          }
        },
        trigger:"change"
        }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
        code: [{ required: true, message: "请输入图形码", trigger: "change" }],
        rcode: [{ required: true, message: "请输入验证码", trigger: "change" }],
      },
      isshow: false,
      imageUrl: "",
      codeurl:"/captcha?type=sendsms",
      baseurl: process.env.VUE_APP_URL,
      
    };
  },
 
  methods: {
    handleAvatarSuccess(res) {
      this.imageUrl = res.data.file_path;
      this.form.avatar=res.data.file_path;
      this.$refs.form.validateField(["avatar"])
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    sblink(){
      this.$refs.form.validate(resule=>{
        if(resule){
          toregister(this.form).then(res=>{
            console.log(res);
          this.isshow=false
          this.$message.success('注册成功')
          })
        }else{
          this.$message.error('注册失败')
        }
      })
    },
  codeimgclick(){
    this.codeurl='/captcha?type=sendsms&ofkokf='+Date.now()
  },
    getcode(){
      let num=0
      this.$refs.form.validateField(["phone","code"],err=>{
        if(err==''){
          num++
        }
      })
      if(num==2){
        // this.$axios({
        //   url:"/sendsms",
        //   method:"post",
        //   data:{
        //     code:this.form.code,
        //     phone:this.form.phone,
        //   }
        // })
        this.totalTime--
        let _interval=setInterval(()=>{
          this.totalTime--
          if(this.totalTime==0){
            this.totalTime=5
            clearInterval(_interval)
          }
        },1000)
        getphonecode({
           code:this.form.code,
            phone:this.form.phone,
        }).then(res=>{
          this.$message.success(res.data.data.captcha+'')
          console.log(res);
        })
      }else{
        this.$message.error('请输入手机号和图形码')
      }
    }
  },
};
</script>

<style lang="less">
.register {
  .el-dialog__header {
    padding: 0;
  }
  .title {
    width: 100%;
    height: 53px;
    color: #ffffff;
    text-align: center;
    background: linear-gradient(#1493fa, #01c6fa);
    line-height: 53px;
    font-size: 14px;
  }
  .footer {
    text-align: center;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.lable-img {
  text-align: center;
}
.img-code{
  width: 100%;
  height: 40px;
}

</style>